package com.ruima.study.arithmetic.sortdemo.practice;

/**
 * <p>
 * <p>原地排序三个相同元素到一起</p>
 * <p>
 * <p>Copyright (c) 2024/11/15 17:40, 科大讯飞 All Rights Reserve</p>
 *
 * @author ruima6
 * @version [V1.0]
 */
public class demo7 {
    public static void main(String[] args) {
        int[] arr = {1,2,1,2,3,3};
        swap(arr,1,2,3);
        swap(arr,2,3,1);
        for (int i : arr) {
            System.out.println(i);
        }
    }

    public  static void swap(int[] arr,int leftValue,int rightValue,int exceptValue){
        int left = 0;
        int right  = arr.length - 1;

        while (left < right){
            if (arr[left] == exceptValue){
                left++;
                continue;
            }
            if (arr[right] == exceptValue){
                right--;
                continue;
            }
            if (arr[left] == rightValue && arr[right] == leftValue){
                int temp = arr[left];
                arr[left] = arr[right];
                arr[right] = temp;
                left++;
                right--;
            }else if (arr[left] == rightValue && arr[right] == rightValue){
                right--;
            }else if (arr[left] == leftValue && arr[right] == leftValue){
                left++;
            }else {
                left++;
                right--;
            }
        }
    }

}
